Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require static importing of some com.fasterxml.jackson.annotation enums #910

Merged
merged 4 commits into from
Dec 18, 2023

Conversation

arichapn
Copy link
Contributor

@arichapn arichapn commented Dec 7, 2023

Added are:

  • JsonCreator.Mode
  • JsonFormat.Shape
  • JsonInclude.Include
  • JsonProperty.Access

@arichapn arichapn requested a review from rickie December 7, 2023 13:55
Copy link

github-actions bot commented Dec 7, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@@ -57,6 +57,7 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa
@VisibleForTesting
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_TYPES =
ImmutableSet.of(
"com.fasterxml.jackson.annotation.JsonCreator.Mode",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's more cases like:

  • @JsonFormat.Shape.
  • @JsonTypeInfo.As.
  • @JsonTypeInfo.Id.
  • @JsonSubTypes.Type.
  • (probably more)

Do you think we could find a way to handle these in bulk @rickie? :)

Or would you recommend listing (and testing) each one individually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we do not need to test all of them. The check is already sufficiently tested.

We can simply add the members to the list. I'm not sure if we should just add all of them. We only add the ones where statically importing doesn't make the code "less readable/understandable", as in, we do not lose any context on what is happening.

  • The JsonFormat.Shape seems like a no-brainer that we should add.
  • For .Id I"m not sure: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, I'd say, JsonTypeInfo can be omitted but the Id part is maybe still "fine" to have? Same holds for:
@JsonTypeInfo(
    use = NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,

We can drop JsonTypeInfo but having .As is maybe better? This is a bit up for discussion I feel. WDYT?

  • I'd argue that @Type is not that clear where it comes from. On the other hand, all usages are only nested in the @JsonSubTypes annotation (I think) and in that case it would make sense.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your proposal 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the annotations from the docs, filtered down to the inner classes, and then did quick scans of some uses in public code on GitHub.

If we ignore JsonCreator.Mode as it's already addressed, and filter down to only the cases where I primarily saw uses where the qualification provides superfluous context, these are what we're left with:

I'd vote for including those in this PR with the same approach as what's currently here for JsonCreator.Mode.


I also looked at the other types Enric mentioned, and agree with Rick:

  • For JsonTypeInfo.As and JsonTypeInfo.Id, I think we should keep around the As and Id bits because of code like this
  • For JsonSubTypes.Type, I think we should keep around the Type bit because of code like this

So to summarise, my vote is to add the following changes here:

  • JsonFormat.Shape.X --> X
  • JsonInclude.Include.X --> X
  • JsonProperty.Access.X --> X
  • JsonTypeInfo.As.X --> As.X
  • JsonTypeInfo.Id.X --> Id.X
  • JsonSubTypes.Type.X --> Type.X

What do you guys think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And as for testing, I'd say what we already have provides sufficient confidence for the cases where we're removing all qualification. But it's probably a good idea to add a test for one of the cases where we're leaving some qualification (e.g. JsonTypeInfo.As.X --> As.X).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw Enric's emoji response. 😄

Curious to hear your thoughts too @rickie, if this all sounds good then I'll proceed. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree with the proposal of which ones to statically import. W.r.t. the ones that are still qualified but not statically imported, I'm not sure if that would fit in this check TBH. I think that should go in the NonStaticImport as there we qualify which types to qualify instead of statically import. Although I'm not sure if that also does this rewrite: JsonTypeInfo.Id.X to Id.X but it does make X -> Id.X. 😄

Nonetheless, I'd say it's worth checking out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, in that case I'll just add the static import options to this PR.

For the imports which remain (at least partially) qualified, I can keep it on my TODO list to check those out. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, let me know what you think. 👍

Copy link

github-actions bot commented Dec 7, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie added this to the 0.15.0 milestone Dec 8, 2023
@arichapn arichapn changed the title StaticImport: add com.fasterxml.jackson.annotation.JsonCreator.Mode StaticImport: add various com.fasterxml.jackson.annotation values Dec 8, 2023
Copy link

github-actions bot commented Dec 8, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for filing a PR @arichapn 🚀!

@rickie
Copy link
Member

rickie commented Dec 11, 2023

Suggested commit message:

Require static import of various `com.fasterxml.jackson.annotation` members (#910)

@rickie rickie force-pushed the arichapn/json-creator-mode-static-imports branch from 320f9b6 to 8a913b7 Compare December 11, 2023 20:46
@rickie rickie requested a review from Stephan202 December 11, 2023 20:46
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 force-pushed the arichapn/json-creator-mode-static-imports branch from 8a913b7 to f8cad7c Compare December 16, 2023 15:00
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased and added some tweaks.

Suggested commit message:

Require static importing of some `com.fasterxml.jackson.annotation` enums (#910)

Types to be imported statically:
- `com.fasterxml.jackson.annotation.JsonCreator.Mode`
- `com.fasterxml.jackson.annotation.JsonFormat.Shape`
- `com.fasterxml.jackson.annotation.JsonInclude.Include`
- `com.fasterxml.jackson.annotation.JsonProperty.Access`

@@ -109,6 +110,12 @@ void identification() {
" @UseImportPolicy(ImportPolicy.IMPORT_TOP_LEVEL)",
" void refasterAfterTemplate() {}",
"",
" // BUG: Diagnostic contains:",
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)",
" private static A create(int a) {",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the method above:

Suggested change
" private static A create(int a) {",
" private static A jsonCreator(int a) {",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To better match the declaration order in StaticImport I'll move this up a bit.)

Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie force-pushed the arichapn/json-creator-mode-static-imports branch from f8cad7c to 03a43a3 Compare December 16, 2023 16:46
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@rickie rickie merged commit 7c2078b into master Dec 18, 2023
16 checks passed
@rickie rickie deleted the arichapn/json-creator-mode-static-imports branch December 18, 2023 06:17
@Stephan202 Stephan202 changed the title StaticImport: add various com.fasterxml.jackson.annotation values Require static importing of some com.fasterxml.jackson.annotation enums Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants